package juc;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class TestCallable {
    public static void main(String[] args) {
        ThreadDemo1 td = new ThreadDemo1();
        long start = System.currentTimeMillis();
        FutureTask<Integer> result = new FutureTask<>(td);
        new Thread(result).start();
        try {
            Integer sum = result.get(); //FutureTask 可用于闭锁
            System.out.println(sum);
            System.out.println("------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("waste: " + (end - start));
    }
}

class ThreadDemo1 implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= 100000000; i++) {
//            System.out.println(i);
            sum += i;
        }
        return sum;
    }
}